package com.console.operations.planmgr;

import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import com.common.test.OperationBase;
import com.common.test.UITestBase;
import com.common.util.DBUtil;
import com.console.pages.planmgr.FinancePlanBondPage;

	/*
	@author 王嘉犇
	@name 理财计划债权提前结清
	@type 操作类
	 */

public class FinancePlanBondRepayInAdvance extends OperationBase {
	
	public FinancePlanBondRepayInAdvance(UITestBase tb){
		super(tb);
	}
	
	//判断提前结清后理财计划债权是否正确
	public boolean isloanstatuscorrected = false;
	
	//判断理财计划债权投资状态是否正确
	public boolean isinveststatuscorrected = false;
	
	public void repayinadvance(String planbondname, String sequenceNo) throws Exception{
		switchToDefaultContent();
		FinancePlanBondPage fpbp = new FinancePlanBondPage(tb);
		waitForElementExist(fpbp.namefuzzysearchinput);
		log.info("planbondname is " + planbondname);
		fpbp.namefuzzysearchinput.type(planbondname);
		fpbp.statusselect.selectOptionByText("还款中");
		pause(800);
		fpbp.searchbutton.clickByJs();
		waitForPageContainsStr(sequenceNo);
		fpbp.detailsLnk.setfindValue("//tr[td[text()='"+ sequenceNo +"']]/td[a[text()='详情']]/a");
		waitForElementExist(fpbp.pagination);
		int pagecount = Integer.parseInt(fpbp.planloanlastPageNumLnk.getText());
		log.info("pagecount is " + pagecount);
		//如果碰到有分页的情况
		if(pagecount>1){
			//遍历每一页，看符合条件的控件是否在页面上，不存在点下一页的页码，存在则点击控件，跳出循环
			for(int i=1;i<pagecount;i++){
				if(isPresent(fpbp.detailsLnk)){
					fpbp.detailsLnk.click();
					break;
				}else{
					fpbp.pageLink.setfindValue("//li[a[text()='"+ (i+1) +"']]/a");
					fpbp.pageLink.click();
					pause(1000);
				}
			}
		}else{
			waitForElementDisplayed(fpbp.detailsLnk);
			fpbp.detailsLnk.click();
		}
		waitForElementExist(fpbp.repayinadvanceorinoverdueLnk);
		fpbp.repayinadvanceorinoverdueLnk.click();
		waitForElementExist(fpbp.repayinadvancebutton);
		fpbp.repayinadvancebutton.click();
		waitForElementExist(fpbp.assureButton);
		fpbp.assureButton.click();
		waitForPageContainsStr(planbondname + "（完成）");
		String status = DBUtil.queryOneCell("select dc_status\n" +
											"from fiz_loan\n" +
											"where vc_sequence = ?", new Object[]{sequenceNo});
		//判断债券状态是否为完成
		isloanstatuscorrected = status.equals("99");
		if(!isloanstatuscorrected){
			log.error("债权的状态不为99(完成)");
			return;
		}
		List<Map<String, String>> investmapLst;
		investmapLst = DBUtil.queryDataLst("select *\n" +
									 	   "from fiz_invest\n" +
										   "where fk_loan_id = (\n" +
															   "select pk_id\n" +
															   "from fiz_loan\n" +
															   "where vc_sequence = ?\n" +
															  ")\n" +
										   "order by dt_datetime desc", new Object[]{sequenceNo});
		Set<String> investstatusset = new HashSet<String>();
		for(Map<String, String> investmap : investmapLst){
			investstatusset.add(investmap.get("dc_status"));
		}
		isinveststatuscorrected = investstatusset.contains("99");
		if(!isinveststatuscorrected){
			log.error("债权投资的状态不含有99(完成)");
			return;
		}
	}
}
